#!/bin/sh
#++++++++++++++++++++ 1. 获取当前系统的桌面名称和当前系统密码++++++++++++++++++++++++++++++
if [ ! -d ./源文件 ]; then echo "不存在源文件目录, 请处理后在运行吧" && exit 0;fi
clear && read -p "请输入当前系统密码:" SYSPWD
if [ ! -d $HOME/Desktop ]; then DesktopPath=$HOME"/桌面";else DesktopPath=$HOME"/Desktop";fi

#++++++++++++++++++++ 2. 初始化打包的环境变量++++++++++++++++++++++++++++++++++
NAME=infreeSubScreen	#包名称
OS=UOS
DESC=基于UOS的后屏管理工具软件	#描述
VERSION=1.0	#版本号
ARCH=amd64	#目标系统硬件架构
AUTHOR=Mr.lv	#维护人员
URL=http://www.infree.com.cn/ #公司网址
AppPath=/opt/subScreen #安装目录
LinkAppPath=/usr/share/applications #图标路径
linkName=后屏管理软件  #无纸化主程序的快捷方式名称
AutoExec=~/.config/autostart  #系统启动项的路径
EXE=${AppPath}/startsubScreen.sh #软件主程序的启动脚本
ICO=${AppPath}/.ico_if # 图标全路径
LIB=${AppPath}/lib  #暂时用不上
DebPack=${AppPath}/APP	#AnZhuangHouYaoAnZhuangDeChaJian
RunDir=$(cd $(dirname $0);pwd)  #运行目录
#DebianName=$NAME/DEBIAN #DEBIAN的路径,不需要修改

 #==============================================
 
# /DEBIAN
echo ${SYSPWD} | sudo -S rm -rf ${RunDir}/${NAME} && sudo mkdir -p ${RunDir}/${NAME}/DEBIAN ${RunDir}/${NAME}/opt && sudo chmod -R 777 ${RunDir}/${NAME}
touch ${RunDir}/${NAME}/DEBIAN/control ${RunDir}/${NAME}/DEBIAN/preinst ${RunDir}/${NAME}/DEBIAN/postinst ${RunDir}/${NAME}/DEBIAN/prerm ${RunDir}/${NAME}/DEBIAN/postrm
#sudo chmod -R 777 ${RunDir}/${DEBIAN_Name}

# /DEBIAN/control
echo "Package:${NAME}" >${RunDir}/${NAME}/DEBIAN/control	#包名
echo "Version:${VERSION}" >>${RunDir}/${NAME}/DEBIAN/control	#版本号
echo "Priority:optional" >>${RunDir}/${NAME}/DEBIAN/control	#优先级：可选
echo "Description:${DESC}" >>${RunDir}/${NAME}/DEBIAN/control	#描述
echo "Architecture:${ARCH}" >>${RunDir}/${NAME}/DEBIAN/control	#体系、架构
echo "Maintainer:${AUTHOR}" >>${RunDir}/${NAME}/DEBIAN/control	#维护人员
sudo scp -r ${RunDir}/源文件/subScreen ${RunDir}/${NAME}/opt

# /DEBIAN/preinst 	安装之前执行的shell脚本（可选）
echo "#!/bin/sh" >${RunDir}/${NAME}/DEBIAN/preinst
echo "sudo killall -9 $NAME" >>${RunDir}/${NAME}/DEBIAN/preinst
echo "exit 0" >>${RunDir}/${NAME}/DEBIAN/preinst

# /DEBIAN/postinst	chuangjian 安装之后执行的shell脚本（可选）
echo "${SYSPWD} | sudo -S rm -rf ${AppPath} && sudo mkdir -p ${AppPath}" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "#!/bin/sh" >${RunDir}/${NAME}/DEBIAN/postinst
echo "echo '$SYSPWD' | sudo -S chmod -R 777 ${AppPath}">>${RunDir}/${NAME}/DEBIAN/postinst
echo "sudo apt install ${DebPack}/*.deb -y" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "sudo apt install -f" >>${RunDir}/${NAME}/DEBIAN/postinst          
echo "sudo rm -rf ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst		# 创建共享的desktop:   /usr/share/applications
echo "sudo touch ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "sudo chmod 777 ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo '[Desktop Entry]' > ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo 'Comment=${DESC}' >> ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo 'Exec=${EXE} %u' >> ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo 'Icon=${ICO}' >> ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo 'Name=${linkName}' >> ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo 'Terminal=false' >> ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo '#Terminal=ture' >> ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo 'Type=Application' >> ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo "echo 'X-Deepin-Vendor=user-custom' >> ${LinkAppPath}/${linkName}.desktop" >>${RunDir}/${NAME}/DEBIAN/postinst
echo ln -s ${LinkAppPath}/${linkName}.desktop ${DesktopPath}/${linkName} >>${RunDir}/${NAME}/DEBIAN/postinst	#创建桌面的快捷方式
echo ln -s ${LinkAppPath}/${linkName}.desktop ${AutoExec}/${linkName} >>${RunDir}/${NAME}/DEBIAN/postinst	#创建启动项的快捷方式
echo exit 0 >>${RunDir}/${NAME}/DEBIAN/postinst


# /DEBIAN/prerm	卸载之前执行的shell脚本（可选）
echo "#!/bin/sh" >${RunDir}/${NAME}/DEBIAN/prerm
echo "sudo killall -9 $NAME" >>${RunDir}/${NAME}/DEBIAN/prerm
echo "exit 0" >>${RunDir}/${NAME}/DEBIAN/prerm

# /DEBIAN/postrm	卸载之后执行的shell脚本（可选）
echo "#!/bin/sh" >${RunDir}/${NAME}/DEBIAN/postrm
echo "sudo rm -rf ${AppPath}" >>${RunDir}/${NAME}/DEBIAN/postrm
echo "rm -rf $LinkAppPath/$linkName.desktop" >>${RunDir}/${NAME}/DEBIAN/postrm	#删除/usr/share/applications无纸化的快捷方式
echo "rm -rf $DesktopPath/$linkName.desktop" >>${RunDir}/${NAME}/DEBIAN/postrm	#删除桌面的快捷方式的软链接
echo "rm -rf $AutoExec/$linkName.desktop" >>${RunDir}/${NAME}/DEBIAN/postrm	#删除启动项的快捷方式的软链接
echo "exit 0" >>${RunDir}/${NAME}/DEBIAN/postrm


sudo chmod -R 755 ${RunDir}/${NAME}
dpkg -b ${RunDir}/${NAME} ${NAME}.${OS}.deb
sudo rm -rf ${RunDir}/${NAME}
#================



